package com.my.jli.controller;

import com.my.jli.Service.ExerciseService;
import com.my.jli.Service.HomeworkService;
import com.my.jli.Service.StudentService;
import com.my.jli.entity.ExerciseChapter;
import com.my.jli.entity.Homework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Random;

/**
 * @ClassName StudentController
 * @Description
 * @Author MaYu
 * @Date 2022/1/20 22:34
 * @Version 1.0
 **/
@Controller
public class StudentController {
    private StudentService studentService;
    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    private HomeworkService homeworkService;
    @Autowired
    public void setHomeworkService(HomeworkService homeworkService) {
        this.homeworkService = homeworkService;
    }

    private ExerciseService exerciseService;
    @Autowired
    public void setExerciseService(ExerciseService exerciseService) {
        this.exerciseService = exerciseService;
    }

    @RequestMapping("/index")
    public String login(HttpSession session){
        if (session.getAttribute("student") == null){
            return "login";
        } else {
            return "student/index";
        }
    }

    @RequestMapping("/studentLogout")
    public String logout(HttpSession session){
        session.removeAttribute("student");
        return "login";
    }

    @RequestMapping("/toSign")
    public String toSign(){
        return "student/sign";
    }

    @RequestMapping("/toResource")
    public String toResource(){
        return "student/resource";
    }

    @RequestMapping("/toChangePassword")
    public String toChangePassword(){
        return "student/changePwd";
    }

    @RequestMapping("/toHomework")
    public String toSubmitHomework(){
        return "student/homework";
    }

    @RequestMapping("/toHomeworkDetail")
    public String toHomeworkDetail(Integer hid, Model model){
        Homework homework = homeworkService.getById(hid);
        model.addAttribute("homework", homework);
        return "student/homeworkDetail";
    }

    @RequestMapping("/toChapterList")
    public String toChapterList(){
        return "student/chapter_list";
    }

    /**
     *
     * @param cId 章节id
     */
    @RequestMapping("/toChapterExercise")
    public String toChapterExercise(Integer cId, Model model){
        model.addAttribute("cId", cId);
        return "student/chapter_exercise_list";
    }

    @RequestMapping("/toRandomExercise")
    public String toRandomExercise(){
        List<ExerciseChapter> chapterList = exerciseService.findAllChapter();
        Random random = new Random();
        int index = random.nextInt(chapterList.size());
        int id = chapterList.get(index).getId();
        return "redirect:/toChapterExercise?cId=" + id;
    }

}
